/*
 * The MIT License (MIT)
 *
 * File: main.c
 *
 * Description: Contains the main loop for software.
 *
 * Copyright (c) 2013 Ed Crampton
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "board.h"
#include "uart.h"
#include "stdio.h"
#include "adc.h"

//Name: Initialize
//Description: Initializes Msp430
//Args:
//Returns:
static void Initialize();

//Name: InitializePins
//Description: Initializes Pins on Msp430
//Args:
//Returns:
static void InitializePins();

extern volatile uint8_t gTempReady;

int main()
{
   Initialize();

   UartPuts("Starting Temperature Sensor Test\n\0");

   while(1)
   {

      if(gTempReady == 1)
      {
         //Read Adc Value and Print
         DisplayTemperature();
         gTempReady = 0;
      }

   }

   return 0;
}

//---------------------------------------------
//Function: Initialize()
//---------------------------------------------
static void Initialize()
{
   dint();//disable interrupts
   WDTCTL = WDTPW + WDTHOLD;  // Stop watchdog timer

   BCSCTL1 = CALBC1_1MHZ;   //Calibrate to 1MHZ clock
   DCOCTL = CALDCO_1MHZ;

   InitializePins(); //Set up the pinx
   UartInitialize(); //set up uart
   AdcInitialize();
   eint();
}

//---------------------------------------------
//Function: InitializePins()
//---------------------------------------------
static void InitializePins()
{
   P1OUT = 0x00;    // Initialize all GPIO
   P1DIR = 0xFF & ~(RX_PIN | TEMP_PIN);               // Set all pins but RXD to output
   P1SEL = RX_PIN + TX_PIN;
   P1SEL2 = RX_PIN + TX_PIN;            // Uart function for TXD/RXD pins
   P2DIR = 0xBF; //ping 2.6  is xin

}
